package com.example.springboot.controller.sys;

import com.example.springboot.config.Shiro.security.SpringUtils;
import com.example.springboot.mapper.CmsUserMapper;
import com.example.springboot.model.entity.CmsUser;
import com.example.springboot.model.enums.ResultCodeEnum;
import com.example.springboot.model.form.Result;
import com.example.springboot.model.req.sys.LoginReq;
import com.example.springboot.service.sys.LoginService;
import com.example.springboot.service.sys.SuSmsLogService;
import com.example.springboot.utils.Assert;
import com.example.springboot.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
public class LoginController {

    @Resource
    SuSmsLogService suSmsLogService;

    @Resource
    CmsUserMapper cmsUserMapper;

    @Resource
    private LoginService loginService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public Object login(LoginReq req) {
        return loginService.login(req);
    }

    /**
     * 用户信息
     */
    @PostMapping("/userInfo")
    public Object userInfo(Integer userId) {
        CmsUser cmsUser = cmsUserMapper.selectById(userId);
        return Result.buildSuccess(cmsUser);
    }

    /**
     * 注册
     */
    @RequestMapping(value = "/signIn", method = RequestMethod.POST)
    public Result signIn(LoginReq req) {
        // 参数校验
        Assert.isTrue(StringUtil.hasBlank(
//                req.getMobile(),
                req.getPassword()
//                req.getCode()
                ),
                ResultCodeEnum.CLIENT_PARAM_ERROR);
//        Assert.notTrue(CheckUtils.checkMobile(req.getMobile()), ResultCodeEnum.MOBILE_FORMAT_ERROR);
        //验证码验证
//        Result checkResult = suSmsLogService.checkMobileMessage(req.getMobile(), req.getCode());
//        if (checkResult.success()) {
        //注册
        return loginService.signIn(req);
//        }
//        return checkResult;
    }

    /**
     * 用户修改
     */
    @PostMapping("/updateUserInfo")
    public Result cmsUserEdit(CmsUser cmsUserReq) {
        // 参数校验
        Assert.isTrue(StringUtil.hasBlank(
                cmsUserReq.getId()),
                ResultCodeEnum.CLIENT_PARAM_ERROR);
        int i = cmsUserMapper.updateById(cmsUserReq);
        return Result.buildSuccess();
    }

    /**
     * 注销
     */
    @GetMapping("logout")
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }

    /**
     * @Description: 修改密码
     * @Param: oldPwd, newPwd, request
     * @return: Baserult
     * @Author: weiquan
     * @Date: 2019/7/1
     */
    @PostMapping("/password/update")
    public Object updatePwd(String oldPwd, String newPwd) {
        return loginService.updatePwd(oldPwd, newPwd);
    }

    /**
     * @Description: 重置密码
     */
    @PostMapping("/password/updateDefaultPwd")
    public Result updateDefaultPwd(LoginReq req) {
        if (!SpringUtils.testEnv()) {
            Result checkResult = suSmsLogService.checkMobileMessage(req.getMobile(), req.getCode());
            if (!checkResult.success()) {
                return Result.with(ResultCodeEnum.CHECK_CODE_ERROR);
            }
        }
        return loginService.updateDefaultPwd(req.getMobile());
    }

}
